package com.cloudspace.jindun.activity.xiaoxi;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.error.VolleyError;
import com.cloudspace.jindun.R;
import com.cloudspace.jindun.activity.CustomTitleBaseActivity;
import com.cloudspace.jindun.log.APPLog;
import com.cloudspace.jindun.message.InfoMessage;
import com.cloudspace.jindun.model.NoticeMessageResult;
import com.cloudspace.jindun.network.api.API;
import com.cloudspace.jindun.network.base.ApiCallback;
import com.cloudspace.jindun.user.UserSessionManager;
import com.cloudspace.jindun.utils.ToastMaker;
import com.zhy.autolayout.AutoRelativeLayout;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.rong.imkit.RongIM;
import io.rong.imlib.IRongCallback;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;

public class InformActivity extends CustomTitleBaseActivity {
    @Bind(R.id.tz_back)
    ImageView mTzBack;
    @Bind(R.id.tz_text_title)
    TextView mTzTextTitle;
    @Bind(R.id.tz_rl)
    AutoRelativeLayout mTzRl;
    @Bind(R.id.phone_et_inform)
    EditText mPhoneEtInform;
    @Bind(R.id.tz_btn_sendInform)
    Button mTzBtnSendInform;

    String targetId;

    private static final String TAG = InformActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xiaoxi_inform_send, false);
        ButterKnife.bind(this);
        targetId = getIntent().getStringExtra("targetId");
    }

    @OnClick({R.id.tz_back, R.id.tz_btn_sendInform})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tz_back:
                finish();
                break;
            case R.id.tz_btn_sendInform:
                String informN = mPhoneEtInform.getText().toString();
                if (!TextUtils.isEmpty(informN)) {
                    final InfoMessage infoMessage = new InfoMessage(getResources().getString(R.string.notice),
                            informN, String.format("0人已阅"), String.valueOf(System.currentTimeMillis()),
                            targetId);
                    Message myMessage = Message.obtain(targetId, Conversation.ConversationType.GROUP,
                            infoMessage);
                    RongIM.getInstance().sendMessage(myMessage, getResources().getString(R.string.info),
                            null, new IRongCallback.ISendMessageCallback() {
                                @Override
                                public void onAttached(Message message) {

                                }

                                @Override
                                public void onSuccess(Message message) {
                                    sentNotification(String.valueOf(message.getSentTime()),
                                            message.getTargetId());

                                }

                                @Override
                                public void onError(Message message, RongIMClient.ErrorCode errorCode) {

                                }
                            });
                } else {
                    ToastMaker.showToastShort(R.string.content_empty);
                }

                break;
        }
    }

    private void sentNotification(String msgId, String groupId) {
        APPLog.zxhLog(TAG + " sentNotification:" + msgId);
        API.getInstance().getNoticeApi().sentNotification(TAG,
                msgId, UserSessionManager.getInstance().getUserId(), groupId,
                new ApiCallback<NoticeMessageResult>() {
                    @Override
                    public void handleResult(NoticeMessageResult result, VolleyError error) {
                        APPLog.zxhLog(TAG+" sentNotification:" + result);

                        finish();
                    }
                }.toastActionError());
    }
}
